IF exists(SELECT * FROM dbo.sysobjects WHERE name='r_totaal' AND xtype='FN') 
BEGIN
	DROP FUNCTION r_totaal
END
GO

CREATE function r_totaal(
@DWcijfer float,
@DWmax float,
@EXcijfer float,
@EXmax float)
returns int
as
begin
  declare @res int
  if ((@DWmax > 0) and (@EXmax > 0))
    begin
      if (@DWcijfer < 0) set @res = studiebegeleiding.dbo.r_procent(@EXcijfer,@EXmax)
      	else if (@EXcijfer < 0) set @res = studiebegeleiding.dbo.r_procent(@DWcijfer,@DWmax)	
      	  else set @res = studiebegeleiding.dbo.r_procent(@DWcijfer + @EXcijfer,
				@DWMax + @EXmax)
    end
  else
    begin
      if (@Exmax <= 0) set @res = studiebegeleiding.dbo.r_procent(@DWcijfer,@DWmax)
      if (@DWmax <= 0) set @res = studiebegeleiding.dbo.r_procent(@EXcijfer,@EXmax)
    end
  return @res
end
------------